<?php

declare (strict_types=1);

namespace App\Model;

/**
 * @property int $id
 * @property int $order_id
 * @property int $uid
 * @property int $aid
 * @property int $goods_id
 * @property string $price
 * @property string $pay_price
 * @property string $currency
 * @property int $type
 * @property int $language
 * @property int $status
 * @property int $evaluate_status
 * @property string $refund_amount
 * @property int $create_time
 * @property int $use_time
 * @property int $start_time
 * @property int $end_time
 * @property int $pre_start_time
 * @property int $pre_end_time
 * @property int $pre_time
 * @property int $pre_confirm_time
 * @property int $delete_time
 */
class OrderGood extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'order_goods';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $guarded = [];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = ['id' => 'integer', 'order_id' => 'integer', 'uid' => 'integer', 'aid' => 'integer', 'goods_id' => 'integer', 'type' => 'integer', 'language' => 'integer', 'status' => 'integer', 'evaluate_status' => 'integer', 'create_time' => 'integer', 'use_time' => 'integer', 'start_time' => 'integer', 'end_time' => 'integer', 'pre_start_time' => 'integer', 'pre_end_time' => 'integer', 'pre_time' => 'integer', 'pre_confirm_time' => 'integer', 'delete_time' => 'integer'];

    public static function addOrderGoods($orderInfo)
    {


        $exists = self::where(['order_id' => $orderInfo['id']])->exists();

        //已经分配过
        if ($exists) {
            return true;
        }

        $language = UserDatum::where(['uid' => $orderInfo['uid']])->value('language');

        for ($i = 0; $i < $orderInfo['num']; $i++) {
            $data[] = [
                'order_id' => $orderInfo['id'],
                'order_no' => self::getOrderNo(),
                'uid' => $orderInfo['uid'],
                'aid' => $orderInfo['aid'],
                'goods_id' => $orderInfo['goods_id'],
                'price' => $orderInfo['price_unit'],
                'pay_price' => $orderInfo['pay_price_unit'],
                'currency' => $orderInfo['currency'],
                'chat_type' => $orderInfo['chat_type'] ?? 1,
                'type' => 1,
                'language' => $language ?? 1,
                'status' => 0,
                'create_time' => time()
            ];
        }
        $rel = false;
        if (!empty($data)) {
            $rel = OrderGood::insert($data);
        }

        if ($rel) {
            return true;
        } else {
            return false;
        }


    }


    public static function getOrderNo()
    {

        $orderNo = 'g' . date('ymd', time()) . substr(microtime(), 2, 3) . sprintf('%05d', rand(0, 99999999));
        $exists = self::where(['order_no' => $orderNo])->exists();
        if ($exists) {
            self::getOrderNo();
        } else {
            return $orderNo;
        }

    }



    public static function goodsOrderList($where,$status){

        $select = [
            'a.id',
            'a.order_id',
            'a.order_no',
            'a.uid',
            'a.aid',
            'a.goods_id',
            'a.status',
            'a.evaluate_status',
            'a.evaluate_status',
            'a.pre_start_time',
            'a.pre_end_time',
            'd.username as num',
            'b.name as nickname',
            'b.type',
            'b.direction',
            'b.language',
            'b.cover',
            'd.online',
            'd.service_status',
            'c.self_name as countryName',
            'c.country_img'
        ];


        $model = ConsultantDatum::from('order_goods as a')
            ->leftJoin('consultant_datum as b','a.aid','=','b.uid')
            ->leftJoin('country as c', 'b.country', '=', 'c.id')//国家表
            ->leftJoin('chat_user as d', 'b.uid', '=', 'd.uid')
            ->where($where)
            ->whereIn('a.status',$status)
            ->select($select)
            ->orderBy('a.id', 'desc');


        $list = page($model);


        return $list;

    }


}